package com.wanmait.demo.filter;

import com.wanmait.demo.pojo.Userinfo;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//登录检查过滤器请求个人中心的所有的路径都需要用户登录
@WebFilter("/user/*")
public class UserCheckLoginFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        HttpSession session = request.getSession();
        Userinfo userinfo = (Userinfo)session.getAttribute("loginUser");
        if(userinfo==null)
        {
            response.sendRedirect(request.getContextPath()+"/loginpager");
            return;
        }
        chain.doFilter(req, resp);
    }


}
